//SharePoint Error Reporting
//By Alon Havivi(havivi@gmail.com, http://havivi.blogspot.com/) 
//For full source code and Terms Of Use,
//visit http://ser.codeplex.com/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SER
{
    class SharePointErrorReporting : SPFeatureReceiver
    {

        private static string LIST_DESCRIPTION_iD_Foutrapportage = "SharePoint Error Reporting provides error and debug information to the site administrator. by Alon Havivi http://havivi.blogspot.com/";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWebApplication webApp = SPAdministrationWebApplication.Local;

            using (SPSite site = webApp.Sites[0])
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPListTemplateType listTemplateType = new SPListTemplateType();

                    bool ListExist = ContainList(web, "Error Reporting");
                    if (!ListExist)
                    {
                        listTemplateType = SPListTemplateType.GenericList;
                        web.AllowUnsafeUpdates = true;
                        web.Lists.Add("Error Reporting",
                                      LIST_DESCRIPTION_iD_Foutrapportage,
                                      listTemplateType);
                        AddFieldsToFoutrapportageList(web);
                    }
                    SPList oList = web.Lists["Error Reporting"];
                    oList.Hidden = true;
                    oList.Update();
                }

            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        private static bool ContainList(SPWeb oSPWeb, string list)
        {
            SPListCollection _lists = oSPWeb.Lists;
            foreach (SPList _list in _lists)
            {
                if (_list.Title.Equals(list))
                {
                    return true;
                }
            }
            return false;
        }

        private static void AddFieldsToFoutrapportageList(SPWeb oSPWeb)
        {
            oSPWeb.AllowUnsafeUpdates = true;
            SPList oList = oSPWeb.Lists["Error Reporting"];
            SPFieldCollection collFields = oList.Fields;

            string userFieldName = collFields.Add("User", SPFieldType.User, false);
            SPField userfField = collFields.GetFieldByInternalName(userFieldName);

            string usernameFieldName = collFields.Add("Username", SPFieldType.Text, false);
            SPField usernameField = collFields.GetFieldByInternalName(usernameFieldName);

            string PCFieldName = collFields.Add("PC", SPFieldType.Text, false);
            SPField PCField = collFields.GetFieldByInternalName(PCFieldName);

            string IPFieldName = collFields.Add("IP", SPFieldType.Text, false);
            SPField IPField = collFields.GetFieldByInternalName(IPFieldName);

            string NETFieldName = collFields.Add(".NET Framework", SPFieldType.Text, false);
            SPField NETField = collFields.GetFieldByInternalName(NETFieldName);

            string BrowserFieldName = collFields.Add("Browser", SPFieldType.Text, false);
            SPField BrowserField = collFields.GetFieldByInternalName(BrowserFieldName);

            string BSFieldName = collFields.Add("Browser Supports", SPFieldType.Text, false);
            SPField BSField = collFields.GetFieldByInternalName(BSFieldName);

            string ApplicationsFieldName = collFields.Add("Applications", SPFieldType.Text, false);
            SPField ApplicationsField = collFields.GetFieldByInternalName(ApplicationsFieldName);

            string LanguageFieldName = collFields.Add("Language", SPFieldType.Text, false);
            SPField LanguageField = collFields.GetFieldByInternalName(LanguageFieldName);

            string WebfrontFieldName = collFields.Add("Webfront", SPFieldType.Text, false);
            SPField WebfrontField = collFields.GetFieldByInternalName(WebfrontFieldName);

            string urlReferrerFieldName = collFields.Add("Url Referrer", SPFieldType.URL, false);
            SPField urlReferrerField = collFields.GetFieldByInternalName(urlReferrerFieldName);

            string siteReferrerFieldName = collFields.Add("Site Referrer", SPFieldType.Text, false);
            SPField siteReferrerField = collFields.GetFieldByInternalName(siteReferrerFieldName);

            string ProbleemFieldName = collFields.Add("Error Description", SPFieldType.Note, false);
            SPField ProbleemField = collFields.GetFieldByInternalName(ProbleemFieldName);

            string ToelichtingFieldName = collFields.Add("User Remarks", SPFieldType.Note, false);
            SPField ToelichtingField = collFields.GetFieldByInternalName(ToelichtingFieldName);

            SPView oView = oList.DefaultView;
            SPViewFieldCollection collViewFields = oView.ViewFields;

            collViewFields.Add(userfField);
            collViewFields.Add(usernameField);
            collViewFields.Add(PCField);
            collViewFields.Add(IPField);
            collViewFields.Add(NETField);
            collViewFields.Add(BrowserField);
            collViewFields.Add(BSField);
            collViewFields.Add(ApplicationsField);
            collViewFields.Add(LanguageField);
            collViewFields.Add(WebfrontField);
            collViewFields.Add(urlReferrerField);
            collViewFields.Add(siteReferrerField);
            collViewFields.Add(ProbleemField);
            collViewFields.Add(ToelichtingField);
            oView.Update();

        }
      
    }

}


